package imdbfolder.gui;

import imdbfolder.app.Application;
import imdbfolder.data.SortMode;
import imdbfolder.workers.MyWorker;

import javax.swing.*;
import java.io.File;
import java.util.prefs.Preferences;

public class ImdbFolderController {

    private final ImdbFolderWindow window;

    public ImdbFolderWindow getWindow() {
        return window;
    }

    private File selectedFolder;

    private MovieListController movieListController;

    public MovieListController getMovieListController() {
        return movieListController;
    }

    private static ImdbFolderController instance;

    public static ImdbFolderController getInstance() {
        return instance;
    }

    public ImdbFolderController() {
        this.window = new ImdbFolderWindow(this);

        instance = this;
    }

    public void initalize() {
        movieListController = new MovieListController();

        window.initialize();
        window.setVisible(true);

        setDefaultFolder();
        loadSelectedFolder();
    }

    private void setDefaultFolder() {
        String lastFolder = Preferences.userRoot().get("imdbfolder.last_folder", "");

        System.out.println("Last selected folder: " + lastFolder);

        if (!lastFolder.equals("")) {
            File file = new File(lastFolder);
            if (file.exists()) {
                setFolderSelection(file);
            }
        }
    }

    public void browseForFolder() {
        Application.cancelWorkers();

        JFileChooser fileChooser;

        if (selectedFolder != null && selectedFolder.exists()) {
            fileChooser = new JFileChooser(selectedFolder);
        }
        else {
            fileChooser = new JFileChooser();
        }

        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int dialogResult = fileChooser.showDialog(window, "Choose folder");

        if (dialogResult == JFileChooser.APPROVE_OPTION) {
            setFolderSelection(fileChooser.getSelectedFile());
            loadSelectedFolder();
        }
    }

    void setFolderSelection(File selectedFolder) {
        if(!selectedFolder.exists()) {
            throw new IllegalArgumentException("Folder doesn't exist: " + selectedFolder.getAbsolutePath());
        }

        this.selectedFolder = selectedFolder;
        Preferences.userRoot().put("imdbfolder.last_folder", selectedFolder.getAbsolutePath());

        window.onFolderSelected(selectedFolder);
    }

    public void loadSelectedFolder() {
        if (selectedFolder != null && selectedFolder.exists()) {
            movieListController.loadFolder(selectedFolder);
        }
    }

    public void registerWorker(MyWorker<?, ?> worker) {
        window.setStatus(worker.getTitle());
    }

    public void deregisterWorker(MyWorker<?, ?> worker) {

    }

    public void cancelAllTasks() {
        Application.cancelWorkers();

        window.setStatus("Tasks cancelled");
    }

    public File getSelectedFolder() {
        return selectedFolder;
    }

    public void searchText(String text) {
        movieListController.setSearchText(text);
        movieListController.displayList();
    }

    public void setSortMode(SortMode sortMode) {
        movieListController.setSortMode(sortMode);
        movieListController.displayList();
    }

}
